/*
package com.wyh.service;

import com.wyh.entity.Account;
import com.wyh.mapper.UserMapper;
import com.wyh.service.AuthorizeService;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AuthorizeService implements UserDetailsService {
    @Resource
    UserMapper mapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if(username == null){
            throw new UsernameNotFoundException("用户名不能为空");
        }
        Account account = mapper.findAccountByNameOrEmail(username);
        if(account == null){
            throw new UsernameNotFoundException("用户名或者密码错误");
        }
        return User
                .withUsername(account.getUsername())
                .password(account.getPassword())
                .roles("user")
                .build();
    }

}
*/
package com.wyh.service.impl;

import com.wyh.entity.Account;
import com.wyh.mapper.UserMapper;
import com.wyh.mapper.captchaMapper;
import com.wyh.service.AuthorizeService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Random;

@Service
public class AuthorizeServiceImpl implements AuthorizeService {

    @Value("${spring.mail.username}")
    String from;

    @Resource
    private UserMapper usermapper; // 注意这里使用了private访问修饰符

    @Resource
    private  captchaMapper captchamapper;

    @Resource
    MailSender mailSender;

    @Override
    public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
        if (email == null) {
            throw new UsernameNotFoundException("邮箱不能为空");
        }
        Account account = usermapper.findAccountByNameOrEmail(email);
        if (account == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        return User
                .withUsername(account.getUsername())
                .password(account.getPassword())
                .roles("user")
                .build();
    }

    /*
    1.生成验证码
     */

    @Override
    public String sendValidateEmail(String email) {
        //String key = "email:" + email;
        Random random = new Random();
        int code = random.nextInt(899999) + 100000;
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email);
        message.setSubject("您的验证邮件");
        message.setText("验证码是：" + code);
        try {
            mailSender.send(message);
            captchamapper.insertCaptchaByEmail(email, String.valueOf(code));
            return null;
        } catch (MailException e) {
            e.printStackTrace();
            return "请确认邮箱地址是否正确，有如有疑问请寻找管理员";
        }

    }

    @Override
    public String validateAndRequest(String username, String password, String email, String code) {
        // String key = "email:" +email;
        if (username == null || password == null || email == null || code == null) {
            return "不能有空值";
        }
        //验证数据库中是否有key
        String c = String.valueOf(captchamapper.findCaptchaByEmail(email));

        if (c.equals(code)) {
            captchamapper.deleteCaptcha(email);
            Account account = new Account();
            account.setUsername(username);
            account.setEmail(email);
            account.setPassword(password);
            usermapper.addUser(account);
            return " 注册成功 ";
        } else {
            captchamapper.deleteCaptcha(email);
            return " 注册失败，请重新获取验证码 ";
        }

    }


    @Bean
    public AuthenticationManager authenticationManager(HttpSecurity security) throws Exception {
        return security
                .getSharedObject(AuthenticationManagerBuilder.class)
                .userDetailsService(this)
                .and()
                .build();
    }



}